/**
 * 渠道运营－邀请经纪人
 */
define(function (require, exports, module) {
    require('authorize');
    var $ = require("jquery");
    var angular = require("angular");
    require("angular-interceptor");
    require("permission");
    require("pagination");
    require("angular-ui");
    require('global');
    var util = require('util');
    var ui = require("ui");
    var dialog = require("dialog");
    var api_prefix = G_CONFIG.api_prefix + "qmyx/broker/";

    angular.module('app', ['ngUI', 'ngInterceptor'])
        .controller('invitationController', ['$rootScope', '$scope', '$http',
            function ($rootScope, $scope, $http) {
                //搜索
                $scope.search = {
                    queryKey: "parent_broker_name",
                    keyword: "",
                    fieldList: [{
                        value: 'parent_broker_name',
                        text: '经纪人姓名'
                    }, {
                        value: 'parent_mobile_tel',
                        text: '经纪人电话'
                    }]
                };
                //筛选
                $scope.filter = {
                    keyword: '',
                    queryKey: 'broker_name',
                    pageSize: 10,
                    startDate: '',
                    endDate: '',
                    // 撤销过滤
                    clearFilter: function () {
                        $scope.filter.startDate = '';
                        $scope.filter.endDate = '';
                        $scope.getFilterData();
                    }
                }
                function doGetData(params) {
                    if (!params) {
                        params = {};
                    }
                    params = $.extend(params, {"USE_FORM_DATA": false});
                    $scope.loading = true;
                    var url = api_prefix + "query-invite-broker-page-list&pageIndex=" + ($scope.pageIndex - 1) + "&pageSize=" + $scope.filter.pageSize;
                    $http
                        .post(url, params)
                        .then(function (res) {
                            $scope.loading = false;
                            var retCode = res.data.retCode;
                            if (retCode == 0) {
                                $scope.data = res.data.data;
                                $scope.list = $scope.data.items;
                                $scope.total = $scope.data.total;
                            } else {
                                ui.showErrTip(res.data.errMsg);
                                return false;
                            }
                        })
                        .finally(function () {
                            $scope.loading = false;
                        });
                };
                $scope.getDataPage = function (pageIndex) {
                    getSearchData(pageIndex - 1)
                };

                var useFilter = true;
                //查询邀请经纪人
                var getSearchData = function (pageIndex) {
                    useFilter = false;
                    pageIndex = pageIndex || 1;
                    var params = {
                        search_text: $scope.search.keyword,
                        search_field: $scope.search.queryKey
                    };
                    $scope.pageIndex = pageIndex;
                    doGetData(params);
                };
                $scope.getSearchData = getSearchData;
                //根据注册时间过滤邀请经纪人
                var getFilterData = function () {
                    useFilter = true;
                    var params = {
                        begin_reg_date: $scope.filter.startDate,
                        end_reg_date: $scope.filter.endDate
                    };
                    doGetData(params);
                }
                $scope.getFilterData = getFilterData;
                //下级经纪人显示隐藏
                $scope.toggleSubBrokers = function (e) {
                    var $target = $(e.target), pb = $target.closest('tr').find('.parent_broker');
                    pb.toggleClass('collapseIc', !pb.hasClass('collapseIc'));
                    $target.closest('tr').nextAll().toggle();
                };

                //检查是否需要另开页面下载
                $scope.beforeExportData = function(params){
                    var taskId = false;
                    $.ajax({
                        type: 'POST',
                        async:false,
                        data:JSON.stringify(params),
                        contentType:'application/json;charset=UTF-8',
                        url: api_prefix + "check-export-invite-broker-need-task&token="+util.getToken(),
                        success: function(re) {
                            if(re.data.needTask){
                                taskId = re.data.taskId;
                            }
                        },
                        error:function(){

                        }
                    });
                    return taskId;
                };

                //导出
                $scope.exportData = function (e) {
                    var params = useFilter ? {
                        begin_reg_date: $scope.filter.startDate,
                        end_reg_date: $scope.filter.endDate
                    } : {
                        search_text: $scope.search.keyword,
                        search_field: $scope.search.queryKey
                    };
                    var task = $scope.beforeExportData(params);

                    if(!task){
                        util.download(api_prefix + "export-invite-broker&token=" + util.getToken(), {filter: JSON.stringify(params)});
                    }else{
                        window.open('/page/export_data.shtml?taskId='+task)
                    }

                    //$(e.target).attr('href', api_prefix + "export-invite-broker&" + $.param(params));
                };
                getSearchData();
            }
        ]);
    angular.bootstrap(document, ['app']);
})